﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Presentation.Module.Security.Interfaces.ViewModels;
using System.ComponentModel;
using Presentation.Module.Security.Model;
using Presentation.Module.Security.Interfaces.Services;
using Microsoft.Practices.Prism.Regions;
using Presentation.Module.Security.Interfaces.Views;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using System.Collections.ObjectModel;

namespace Presentation.Module.Security.ViewModels
{
    public class RoleViewModel : UserViewModelBase, IRoleViewModel, INotifyPropertyChanged
    {
        private Role _currentRole;

        private IUserService _userService;

        private IRegionManager _regionManager;

        private IAddRoleView _addRoleView;

        public ICommand AddRole { get; private set; }

        public ICommand EditRole { get; private set; }

        public ICommand DeleteRole { get; private set; }

        public RoleViewModel(IRegionManager regionManager, IUserService userService, IAddRoleView addRoleView)
            : base(regionManager, userService)
        {
            this.AddRole = new DelegateCommand(this.OnAddRole);
            this.EditRole = new DelegateCommand<Role>(this.OnEditRole);
            this.DeleteRole = new DelegateCommand<Role>(this.OnDeleteRole);

            this._regionManager = regionManager;
            this._userService = userService;
            this._addRoleView = addRoleView;
            this._regionManager.Regions["MainRegion"].Add(addRoleView);

            this._addRoleView.ViewExit += this.ChildViewExit;

            _currentRole = new Role(); 
        }

        public Role CurrentRole
        {
            get
            {
                return this._currentRole;
            }

            set
            {
                if (value == null)
                {
                    return;
                }

                this._currentRole = value;
                this.RaiseOnPropertyChanged("Roles");
            }
        }

        public ObservableCollection<Role> Roles
        {
            get { return this._userService.GetAllRoles(); }
        }

        public void OnAddRole()
        {
            _regionManager.Regions["MainRegion"].Activate(this._addRoleView);
        }

        public void OnEditRole(Role role)
        {
            this._userService.EditRole(role);
        }

        public void OnDeleteRole(Role role)
        {
            this._userService.DeleteRole(role);
            this.RaiseOnPropertyChanged("Roles");
        }

        private void ChildViewExit()
        {
            this._regionManager.Regions["MainRegion"].Activate(this.View);
            this.RaiseOnPropertyChanged("Roles");
        }

        protected void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public IRoleView View { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
